<!--vue模板-->
<template>
    <el-row>
        <el-button type="primary" plain @click="visible = true">新增</el-button>
    </el-row>
  <el-row style="background-color: transparent; height: 80vh;">
    <el-col :span="4">
      <CategoryTree ref="categoryRef"></CategoryTree>
    </el-col>
    <el-col :span="20" style="height: 100%">
      <el-table :data="tableData" border style="width: 100vw">
        <el-table-column prop="code" label="编码" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="pname" label="父分类名称" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              type="primary"
              :icon="Edit"
              circle
              @click="updateCategory(scope.row)"
            />
            <el-button
              type="danger"
              :icon="Delete"
              circle
              @click="deleteById(scope.row.id)"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>

  <SaveOrUpdate
  ref="saveUpdate"
    @close="refresh"
  ></SaveOrUpdate>

</template>

<!--vue 组件选项定义-->
<script setup>
import { onBeforeMount, provide, ref } from "vue";
import CategoryTree from "./CategoryTree.vue";
import SaveOrUpdate from "./SaveOrUpdate.vue";
import {
  queryCategoryAll,
  queryById,
  deleteCategoryById,
} from "@/api/basic/Category.js";

import { Delete, Edit } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";

const saveUpdate = ref(null)
// 编辑数据
const updateCategory = (row) => {
    visible.value = true
    saveUpdate.value.init(row)
}

// 刷新整个界面的数据
const refresh = () => {
    categoryRef.value.showData();
    getCurCategory()
}

// 新增 添加
const visible = ref(false)

provide('isShow',visible)

const pidAdd = ref('')

provide('pidAdd',pidAdd)

// --------------
//  查出的字典数据
const tableData = ref([]);
const categoryRef = ref(null)

//-----------------
const showCurData = async (id) => {
    pidAdd.value = id
    const res = await queryById(id);
    tableData.value = res.data;
}

provide('showCurData',showCurData)

// ------------------
const pid = ref("");

async function getCurCategory() {
  const res = await queryById(pid.value);

  tableData.value = res.data;
}

onBeforeMount(() => {
  getCurCategory();
});

const deleteById = (id) => {
  console.log("id");

  ElMessageBox.confirm("您将要删除当前用户，是否删除?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteCategoryById(id).then((resp) => {
        ElMessage.success("删除成功！");
        getCurCategory();
        // 刷新数据
        categoryRef.value.showData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除！",
      });
    });
};
</script>

<!--vue样式 scoped当前样式只适用于当前组件-->
<style scoped>

:deep(.el-tree-node__content:hover ){
  background-color: aquamarine;
}
.el-row {
  margin-bottom: 20px;
}
.el-row:last-child {
  margin-bottom: 0;
}
.el-col {
  border-radius: 4px;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
</style>
